package com.example.universita.esami;

import android.os.Bundle;
import android.text.util.Linkify;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;


public class Docenti extends Activity  {

	String spedisci_docente;
	String[] nome_esame;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_docente);
        
        int id_docente;
        String only_name="Nessun nome trovato";
        String nome="Nessun nome trovato";
		String mail="Nessuna mail trovata";
		String meeting="Nessuna ricevimento trovato";
		String phone="Nessun telefono trovato";
		
        Intent intent = getIntent();
        String pkg = getPackageName();
        String message = spedisci_docente = intent.getStringExtra(pkg+".Docente");
        
        DBDataUniversity db = new DBDataUniversity(this); 

        db.open();
        
        Cursor cursor = db.getDettagli_Docenti(message);
      
        if(cursor.moveToFirst()){
        	only_name = cursor.getString(0);
        	nome = cursor.getString(0);
			mail = cursor.getString(1);
			meeting = cursor.getString(2);
			phone = cursor.getString(3);
        }
        cursor.close();
        
        
      
        TextView textview_nome_d = (TextView) findViewById(R.id.nome_docente);
        textview_nome_d.setText(nome);
        
        TextView textview_mail = (TextView) findViewById(R.id.view_mail);
        textview_mail.setText(mail);
        Linkify.addLinks(textview_mail, Linkify.EMAIL_ADDRESSES);
        
        TextView  textview_meeting = (TextView) findViewById(R.id.view_meeting);
        textview_meeting.setText(meeting);
       
        TextView  textview_phone = (TextView) findViewById(R.id.view_phone);
        textview_phone.setText(phone);
        Linkify.addLinks(textview_phone, Linkify.PHONE_NUMBERS); // Riconosce i numeri telefonici e al clic avvia la composizione del numero
       
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.Lista_esami_docente);
        
        Cursor cursor2 = db.getId_da_Docente(only_name);
        
        if(cursor2.moveToFirst()==true)
        	
        {
        	int temp=0;
        	nome_esame= new String[30];
        	id_docente=cursor2.getInt(0);
        
        	cursor2 = db.getEsami_da_Docente(id_docente);
       
        	while(cursor2.moveToNext())
        	{
            TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        	TextView tv= new TextView(this);
        	tv.setId(temp);
        	tv.setLayoutParams(params);
        	tv.setTypeface(null, Typeface.ITALIC);
        	
        	tv.setOnClickListener(new View.OnClickListener() {
				
				public void onClick(View v) {
					
					Intent intent = new Intent (getBaseContext(), Dettagli_esame.class);
					
					String pkg = getPackageName();
					intent.putExtra(pkg+".myString", nome_esame[v.getId()]);
 	    	   		startActivity(intent); 
				}
			});
        	tv.setTextColor(0xFFFFFFFF);
        	tv.setText(cursor2.getString(0));
        	nome_esame[temp]=cursor2.getString(0);
        	linearLayout.addView(tv);
        	temp++;
        	}
        	
        }
        cursor2.close();
        
        db.close();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_docenti, menu);
        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	Intent intent;
    	switch(item.getItemId()){
    	case R.id.dettagli: // Vai a Dettagli Esame
	    	   intent = new Intent (this, Details.class);
			   startActivity(intent);
 	       break; 
    	case R.id.statistiche: // Vai a Dettagli Esame
	    	   	intent = new Intent (this, StatisticActivity.class);
	    	   	startActivity(intent);
	    	   	break;    
	    case R.id.carriera: // Vai a Carriera
	    	   intent = new Intent(this, Carriera.class);
	    	   startActivity(intent);  
 	       break;             
	    case R.id.home: // Vai alla Home
  	   	   	intent = new Intent(this, MainActivity.class);
  	   	   	startActivity(intent);  
  	   	   	break;  
	    case R.id.modifica:
	    	intent = new Intent (getApplicationContext(), EditDocenti.class);
			String pkg = getPackageName();
			intent.putExtra(pkg+".nomeDocente", spedisci_docente);
			intent.putExtra(pkg+".daDove", true);
			startActivity(intent);
			break;
	    	
    	}
     return true;
    }

    
}
